<?php


namespace App\Admin\Controllers;


use App\Constants\ModuleSlugConstant;
use App\Models\Settings;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Models\Permission;

class ApplyController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->header('应用中心')
            ->body(view('admin.apply-centre.index', ['data' => $this->menu()]));
    }

    public function storeSlugNeedVip($module, $slug)
    {
        $val = getSettings($module, $slug) ?? 0;

        Settings::updateOrCreate([
            'group' => $module,
            'name'  => $slug
        ], ['value' => $val == 0 ? 1 : 0]);

        flushSettings($module);

        return response()->json(['status' => true, 'msg' => '操作成功']);
    }

    protected function menu(): array
    {
        $data = [];

        $healthPermission = true;

        if (!Admin::user()->isAdministrator()) {
            $permissions = Permission::query()->whereIn('slug', ['health-acu-manage'])->pluck('id', 'slug')->all();
            if (!$permissions) {
                return $data;
            }

            $permissionParentIds = Admin::user()->allPermissions()->pluck('parent_id');

            $healthPermission = $permissionParentIds->contains($permissions['health-acu-manage']);
        }

        if ($healthPermission && $acuMenu = $this->acuMenu()) {
            $data[] = $acuMenu;
        }

        return $data;
    }

    protected function acuMenu(): array
    {
        $data = [
            'title' => '穴位大全',
            'items' => [],
        ];

        $settings = getSettings(ModuleSlugConstant::XW_MODULE);

        $data['items'][] = [
            'title'    => '穴位管理',
            'module'   => ModuleSlugConstant::XW_MODULE,
            'slug'     => ModuleSlugConstant::XW_ACU_MODULE,
            'need_vip' => $settings[ModuleSlugConstant::XW_ACU_MODULE] ?? 0,
            'items'    => [
                ['title' => '部位', 'url' => admin_route('health.body.index')],
                ['title' => '部位节点', 'url' => admin_route('health.body-point.index')],
                ['title' => '经脉', 'url' => admin_route('health.meridian.index')],
                ['title' => '穴位', 'url' => admin_route('health.acupoint.index')],
            ]
        ];

        $data['items'][] = [
            'title'    => '呼吸法管理',
            'module'   => ModuleSlugConstant::XW_MODULE,
            'slug'     => ModuleSlugConstant::XW_BREATHE_MODULE,
            'need_vip' => $settings[ModuleSlugConstant::XW_BREATHE_MODULE] ?? 0,
            'items'    => [
                ['title' => '爱呼吸', 'url' => admin_route('health.breathe.index')],
            ]
        ];

        $data['items'][] = [
            'title'    => '病症管理',
            'module'   => ModuleSlugConstant::XW_MODULE,
            'slug'     => ModuleSlugConstant::XW_DISEASE,
            'need_vip' => $settings[ModuleSlugConstant::XW_DISEASE] ?? 0,
            'items'    => [
                ['title' => '分类', 'url' => admin_route('health.disease-class.index')],
                ['title' => '病症', 'url' => admin_route('health.disease.index')],
                ['title' => '调理方案', 'url' => admin_route('health.disease-scheme.index')],
            ]
        ];
        $data['items'][] = [
            'title'    => '病症鉴别',
            'module'   => ModuleSlugConstant::XW_MODULE,
            'slug'     => ModuleSlugConstant::XW_DISEASE_DISEASE,
            'need_vip' => $settings[ModuleSlugConstant::XW_DISEASE_DISEASE] ?? 0,
            'items'    => [
                ['title' => '分类', 'url' => admin_route('health.disease-discern-class.index')],
                ['title' => '文章', 'url' => admin_route('health.disease-discern.index')],
            ]
        ];
		$data['items'][] = [
            'title'    => '中医经典',
			'module'   => ModuleSlugConstant::XW_MODULE,
            'slug'     => ModuleSlugConstant::TCM_MODULE,
            'need_vip' => $settings[ModuleSlugConstant::TCM_MODULE] ?? 0,
            'items'    => [
                ['title' => '医案', 'url' => admin_route('tcmcase.index')],
                ['title' => '茶', 'url' => admin_route('tcmtea.index')],
                ['title' => '医家', 'url' => admin_route('tcmdoctor.index')],

				['title' => '董氏奇穴', 'url' => admin_route('tcmdongqipoint.index')],
                ['title' => '药膳', 'url' => admin_route('tcmmedicineddiet.index')],
                ['title' => '食材', 'url' => admin_route('tcmfood.index')],

				['title' => '方剂', 'url' => admin_route('tcmprescription.index')],
                ['title' => '中成药', 'url' => admin_route('tcmmedicine.index')],
                ['title' => '药材', 'url' => admin_route('tcmdrug.index')],

                ['title' => '香', 'url' => admin_route('tcmaroma.index')],
                ['title' => '医书', 'url' => admin_route('tcmbook.index')],
            ]
        ];


        return $data;
    }
}
